//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SoftCallability.h"
using namespace Cephei::QL::Experimental;
#include <gen/QL/Instruments/Callability.h>
using namespace Cephei::QL::Instruments;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::CSoftCallability::CSoftCallability (boost::shared_ptr<QuantLib::SoftCallability>& childNative, Object^ owner) : CCallability(CSoftCallability::typeid)
{
#ifdef HANDLE
	_phSoftCallability = NULL;
#endif
	_ppSoftCallability = &childNative;
    _ppCallability = new boost::shared_ptr<QuantLib::Callability> (boost::dynamic_pointer_cast<QuantLib::Callability> (*_ppSoftCallability));
}
Cephei::QL::Experimental::CSoftCallability::CSoftCallability (QuantLib::SoftCallability& childNative, Object^ owner) : CCallability(CSoftCallability::typeid)
{
#ifdef HANDLE
	_phSoftCallability = NULL;
#endif
	_ppSoftCallability = new boost::shared_ptr<QuantLib::SoftCallability> (&childNative);
    _ppCallability = new boost::shared_ptr<QuantLib::Callability> (boost::dynamic_pointer_cast<QuantLib::Callability> (*_ppSoftCallability));
    _SoftCallabilityOwner = owner;
    _CallabilityOwner = owner;
}

Cephei::QL::Experimental::CSoftCallability::CSoftCallability (CSoftCallability^ copy) : CCallability(CSoftCallability::typeid)
{
#ifdef HANDLE
	_phSoftCallability = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSoftCallability = new boost::shared_ptr<QuantLib::SoftCallability> (copy->GetShared());
        _ppCallability = new boost::shared_ptr<QuantLib::Callability> (boost::dynamic_pointer_cast<QuantLib::Callability> (*_ppSoftCallability));
    }
}
Cephei::QL::Experimental::CSoftCallability::CSoftCallability (PLATFORM::Type^ t) : CCallability(CSoftCallability::typeid)
{
#ifdef HANDLE
	_phSoftCallability = NULL;
#endif
	if (!t->IsSubclassOf(CSoftCallability::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::CSoftCallability::CSoftCallability (QuantLib::Handle<QuantLib::SoftCallability>& childNative, Object^ owner)  : CCallability(CSoftCallability::typeid)
{
	_phSoftCallability = &childNative;
	_ppSoftCallability = &static_cast<boost::shared_ptr<QuantLib::SoftCallability>>(childNative.currentLink());
    _ppCallability = new boost::shared_ptr<QuantLib::Callability> (boost::dynamic_pointer_cast<QuantLib::Callability> (*_ppSoftCallability));
    _SoftCallabilityOwner = owner;
}
Cephei::QL::Experimental::CSoftCallability::CSoftCallability (QuantLib::Handle<QuantLib::SoftCallability> childNative)  : CCallability(CSoftCallability::typeid)
{
	_phSoftCallability = &childNative;
	_ppSoftCallability = &static_cast<boost::shared_ptr<QuantLib::SoftCallability>>(childNative.currentLink());
    _ppCallability = new boost::shared_ptr<QuantLib::Callability> (boost::dynamic_pointer_cast<QuantLib::Callability> (*_ppSoftCallability));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::CSoftCallability::CSoftCallability (QuantLib::SoftCallability childNative)  : CCallability(CSoftCallability::typeid)
{
#ifdef HANDLE
	_phSoftCallability = NULL;
#endif
	_ppSoftCallability = new boost::shared_ptr<QuantLib::SoftCallability> (new QuantLib::SoftCallability (childNative));
    _ppCallability = new boost::shared_ptr<QuantLib::Callability> (boost::dynamic_pointer_cast<QuantLib::Callability> (*_ppSoftCallability));
}
#endif

Cephei::QL::Experimental::CSoftCallability::~CSoftCallability ()
{
    if (_ppSoftCallability != NULL)
    {
	    delete _ppSoftCallability;
        _ppSoftCallability = NULL;
    }
}
Cephei::QL::Experimental::CSoftCallability::!CSoftCallability ()
{
    if (_ppSoftCallability != NULL)
    {
	    delete _ppSoftCallability;
    }
}
QuantLib::SoftCallability& Cephei::QL::Experimental::CSoftCallability::GetReference ()
{
    if (_ppSoftCallability == NULL) throw REFNEW NativeNullException ();
	return **_ppSoftCallability;
}
boost::shared_ptr<QuantLib::SoftCallability>& Cephei::QL::Experimental::CSoftCallability::GetShared ()
{
    if (_ppSoftCallability == NULL) throw REFNEW NativeNullException ();
	return *_ppSoftCallability;
}
QuantLib::SoftCallability* Cephei::QL::Experimental::CSoftCallability::GetPointer ()
{
    if (_ppSoftCallability == NULL) throw REFNEW NativeNullException ();
	return &**_ppSoftCallability;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SoftCallability>& Cephei::QL::Experimental::CSoftCallability::GetHandle ()
{
	if (_phSoftCallability == NULL)
	{
		_phSoftCallability = new Handle<QuantLib::SoftCallability> (*_ppSoftCallability);
	}
	return *_phSoftCallability;
}
#endif
bool Cephei::QL::Experimental::CSoftCallability::HasNative () 
{
	return (_ppSoftCallability != NULL);
}

Double Cephei::QL::Experimental::CSoftCallability::Trigger::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSoftCallability)->trigger ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

